package com.eig.course.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eig.common.entity.course.CheckCourse;
import com.eig.common.entity.course.EduCourse;
import com.eig.common.entity.course.EduSubject;
import com.eig.common.vo.Result;
import com.eig.course.dao.CheckCourseDao;
import com.eig.course.dao.CoreCourseDao;
import com.eig.course.dao.CoreDao;
import com.eig.course.service.CheckCourseService;
import com.eig.course.vo.EduCourseVo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/12/17 下午 09:31
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CheckCourseServiceImpl implements CheckCourseService {

    @Resource
    private CheckCourseDao checkCourseDao;

    @Resource
    private CoreDao coreDao;
    @Resource
    private CoreCourseDao coreCourseDao;

    @Override
    public Result queryByPage(CheckCourse e, PageRequest pageRequest) {

        long total = checkCourseDao.count(e);
        PageImpl<CheckCourse> eduSubjectsL = new PageImpl<>(this.checkCourseDao.queryAllByLimit(e, pageRequest), pageRequest, total);
        Map<String, Object> map = new HashMap<>();
        LinkedList<EduSubject> eduSubjects = coreDao.queryAlls();
        for (CheckCourse course : eduSubjectsL.getContent()) {
            for (EduSubject eduSubject : eduSubjects) {
                if (course.getSubjectId().equals(eduSubject.getId())) {
                    course.setSubjectName("【" + eduSubject.getTitle() + "】");
                }
            }
        }
        for (CheckCourse course : eduSubjectsL.getContent()) {
            for (EduSubject eduSubject : eduSubjects) {
                if (course.getSubjectParentId().equals(eduSubject.getId())) {
                    if (!course.getSubjectParentId().equals("0")) {
                        course.setSubjectName(("【" + eduSubject.getTitle() + "】 >  ") + course.getSubjectName());
                    }
                }
            }
        }
//        if (ObjectUtil.isNotEmpty(pageRequest)){
//            eduSubjectsL.getContent().stream().skip(pageRequest.getOffset()).limit(pageRequest.getPageSize());
//        }
        map.put("records", eduSubjectsL.getContent());
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result(2000, "查询成功", map);
    }

    @Override
    @Transactional
    public Result updateCourseCheck(CheckCourse e) {
        System.out.println(e);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("course_check_id", e.getCourseCheckId());
        if (e.getCheckStatus().equals("1")) {
            updateWrapper.set("check_status", e.getCheckStatus());
            updateWrapper.set("check_status", e.getCheckStatus());
            UpdateWrapper updateWrapper2 = new UpdateWrapper();
            updateWrapper2.eq("id", e.getCourseId());
            updateWrapper2.set("status","Normal");
            updateWrapper2.set("is_deleted","0");
            coreCourseDao.update(null,updateWrapper2);
        } else {
            updateWrapper.set("check_status", e.getCheckStatus());
            UpdateWrapper updateWrapper2 = new UpdateWrapper();
            updateWrapper2.eq("id", e.getCourseId());
            updateWrapper2.set("status","Draft");
            updateWrapper2.set("is_deleted","1");
            coreCourseDao.update(null,updateWrapper2);
        }
        checkCourseDao.update(null,updateWrapper);
        return new Result(2000, "修改成功");
    }
}
